﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //SqlConnection con = new SqlConnection(connectionString);
        //try
        //{
        //    con.Open();
        //    Response.Write(@"<b>服务器版本号:</b>" + con.ServerVersion);
        //    Response.Write(@"<b>服务器状态:</b>" + con.State);
        //}
        //catch (Exception ez)
        //{
        //    Response.Write(@"<b>异常信息:</b>" + ez.Message);
        //}
        //finally
        //{
        //    con.Close();
        //    Response.Write(@"</br><b>服务器状态:</b>" + con.State);
        //}

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //读取选中内容
        int k = GridView1.SelectedIndex;
        string seleteNo = GridView1.SelectedRow.Cells[1].Text;
        string seleteName = GridView1.SelectedRow.Cells[2].Text;
        HttpCookie sc = createShoppingCart();
        string key = seleteNo;
        if (sc[key] != null)
        {//在
            sc[key] = (int.Parse(sc[key]) + 1).ToString();
        }
        else 
        {//不在
            sc[key] = "1";
        }

        Response.Cookies.Add(sc);
    }

    private HttpCookie createShoppingCart()
    {
        HttpCookie hc = null;
        if (Request.Cookies["ShoppingCart"] == null)
        {
            //如果Cookies中不存在ShoppingCart，则创建
            Dictionary<string, int> cart = new Dictionary<string, int>();
            hc = new HttpCookie("ShoppingCart");
        }
        else
        {
            //如果Cookies中存在ShoppingCart，则取出
            hc = Request.Cookies["ShoppingCart"];
        }
        return hc;
    }
}